<%@page import="entities.MetaSeo"%>
<%@page import="systems.ConfigSystem"%>
<%@page import="entities.SiteConfig"%>
<%@page import="java.util.TreeMap"%>
<%@page import="helpers.LanguageHelper"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="helpers.DateHelper"%>
<%@page import="entities.Category"%>
<%@page import="entities.User"%>
<%@page import="java.util.Map"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="helpers.StringHelper"%>
<%@page import="entities.Article"%>
<%@page import="java.util.List"%>
<%
SiteConfig siteConfig = ConfigSystem.getConfigs();

User author = (User) request.getAttribute("author");
List<Article> articles = (List<Article>) request.getAttribute("articles"); 
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories");
Category category = null;

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();

//Paginator
int pages = (Integer) request.getAttribute("page");
boolean nextPage = (Boolean) request.getAttribute("nextPage");
boolean prevPage = (Boolean) request.getAttribute("prevPage");
PaginatorHelper paginator = (PaginatorHelper) request.getAttribute("paginator");

MetaSeo metaseo = (MetaSeo) request.getAttribute("metaseo");

//Set content for Seo
Map<String, String> replates = new TreeMap<String, String>();
replates.put("__SERVER_NAME__", UrlHelper.getDomainServer(request));
replates.put("__SERVER_URL__", UrlHelper.getServerUrl(request));
replates.put("__AUTHOR_NAME__", author.getName());
replates.put("__AUTHOR_JOIN_DATE__", author.getCreated().toString());
replates.put("__AUTHOR_ABOUT__", author.getAboutme());

String title = StringHelper.replateContent(metaseo.getTitle(), replates);
String description = StringHelper.replateContent(metaseo.getDescription(), replates);
String keywords = StringHelper.replateContent(metaseo.getKeywords(), replates);
String url = StringHelper.replateContent(metaseo.getOgurl(), replates);
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/ezsarticle/layouts/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>

		<div class="row-fluid">
			<div class="span8">
				<div class="block">
					<h3 class="title"><%=author.getName() %> info</h3>
					<ul class="list_author">
						<li>
							<div class="clearfix">
								<div class="pull-left author-image">
									<a href="#">
										<% 
											if (author.getAvatar() != null && !author.getAvatar().isEmpty())  {
												 BlobKey blobKey = new BlobKey(author.getAvatar());
										%>
										<img alt="" src="<%=imagesService.getServingUrl(blobKey) %>=s60-c">
										<% } else { %>		
										<img src="/images/icon-author.jpg" width="60">
										<% } %>
									</a>
								</div>
								<div class="pull-left author-info">
									<a href="/author/<%=author.getId()%>" class="author_name"><%=author.getName() %></a>
									<span>Joined: <%=DateHelper.getStringFromDate(author.getCreated(), "MMM dd, yyyy") %></span> | 
									<span>Website: <%=author.getWebsite() %></span>
									<p>
									<strong>About Me: </strong> <%=author.getAboutme() %>
									</p>
								</div>
							</div>
						</li>
					</ul>
				</div>
				<% 
                for (int i = 0; i < articles.size(); i++) {
                	category = categories.get(articles.get(i).getCategory());
                %>
				<article>
					<h2><a href="/article/<%=articles.get(i).getAlias() %>"><%=articles.get(i).getTitle() %></a></h2>
					<p><%=StringHelper.cutString(StringHelper.html2text(articles.get(i).getContent()), 40) %> [...]</p>
					<p class="meta">
						<i class="icon-calendar"></i> <%=DateHelper.getStringFromDate(articles.get(i).getCreated(), "MMM dd, yyyy") %> - 
						<% if (category != null) { %>
						<i class="icon-folder-open"></i> <a href="/category/<%=category.getAlias() %>"><%=category.getName() %></a> - 
						<% } %>
						<% if (author != null) { %>
						<i class="icon-user"></i> <a href="/author/<%=author.getId()%>"><%=author.getName() %></a>
						<% } else if (!articles.get(i).getAuthorName().isEmpty()) { %>
						<i class="icon-user"></i> <%=articles.get(i).getAuthorName() %>
						<% } %>
						<a class="pull-right readmore" href="/article/<%=articles.get(i).getAlias() %>">Read More &raquo;</a>
					</p>
				</article>
				 <% } %>
				 
				 <div class="pagination">
					<%
					List<Integer> range = paginator.getRangePage();
					%>
                    <% if (prevPage) { %>
					<a href="<%=paginator.getPrev()%>" class="prev page-numbers">Prev</a>
					<% } %>
					<%
					if (nextPage || prevPage) {
						for (int i = 0; i < range.size(); i++) {
							if (range.get(i).intValue() == pages) {
					%>
					<span class="page-numbers current"> <%=range.get(i).toString() %></span>
						<%  } else { %>
					<a class="page-numbers" href="<%=paginator.getPage(range.get(i).intValue())%>"> <%=range.get(i).toString() %></a>
					<%  	}
						}
					} %>
					
					<% if (nextPage) { %>
					<a href="<%=paginator.getNext()%>" class="next page-numbers">Next</a></li>
					<% } %>
				</div>
			</div>
			
			<jsp:include page="/templates/ezsarticle/elements/right.jsp" />
			
		</div>

<jsp:include page="/templates/ezsarticle/layouts/footer.jsp" />